package com.pie4cloud.pie.job.executor.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DecodeUtils {
	
	public static Map<String,String> decode(String path,String pathReg,Map<String,String> decodeConfig,Map<String,String> commonMap,String ignoreKeys) throws RegException {

		String[] match = RegUtils.match(pathReg, path);
		Map<String, String> matchMap = arraysToMap(match);
		Map<String,String> result = new HashMap<>();

		for (Map.Entry<String,String> entry:
			 decodeConfig.entrySet()) {
			if (ignoreKeys.contains(entry.getValue())) {
				result.put(entry.getKey(),entry.getValue());
			}
			String value = PathUtils.parse(entry.getValue(), matchMap, commonMap);
			result.put(entry.getKey(),value);

		}
		return result;
	}

	public static Map<String,String> arraysToMap(String[] arrays) {
		Map<String,String> result = new HashMap<>();
		for (int i = 0; i < arrays.length; i++) {
			result.put(i+"",arrays[i]);
		}
		return result;
	}

	public static Map<String,String> getParamsMap(List<Map<String,String>> list) {
		Map<String,String> result = new HashMap<>();
		for (int i = 0; i < list.size(); i++) {
			Map<String, String> map = list.get(i);
			String colCode = map.get("colCode");
			String decodeConfig = map.get("decodeConfig");
			if (StringUtils.isNotBlank(colCode) && StringUtils.isNotBlank(decodeConfig)) {
				result.put(colCode,decodeConfig);
			}
		}
		return result;
	}
	
}
